var targetObj = null;

function setLatestElement(e) {
	var targ = null;
	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;

	//sets the reference to the clicked HTML object
	targetObj = targ;

	//triggers the setting of the badge text with the HTML tag name of the clicked object
	chrome.extension.sendRequest({tagName: targ.tagName}, function (response) 
	{
		console.log(response.done);
	});
}

//fills the clicked element with the text triggered by the browser action
chrome.extension.onRequest.addListener(
  function(request, sender, sendResponse) {
	  txtToUse = request.txt;
	  if (targetObj) {
		try 
		{
			targetObj.innerHTML += txtToUse;
		}
		catch (err) {
			targetObj.value += txtToUse;
		}
	  }
  });


 //attaches the event handler to the click event
if (document.body) {
	document.body.addEventListener("click", setLatestElement);
}